﻿package com.kennycosca.util {
	
	import flash.events.*;
	import flash.net.*;
	import flash.display.*;
	import flash.system.*;
	
	/**
	 * @author kentendo
	 */
	
	
	public class AssetLoader extends EventDispatcher {	
		
		public static const LOADED:String = "loaded";
		public static const FAILED:String = "failed";
		public static const SWF_TYPE:String = "swf_type";
		public static const IMG_TYPE:String = "img_type";
		
		
		
		private var _swfUrl : String;
        private var _loaded : Boolean = false;
        private var _loader : Loader;
        private var _swfContent : *;
        private var _imgContent : *;
        private var _type:String;
        

		public function AssetLoader(swfUrl : String, type:String = SWF_TYPE) {
			trace("AssetLoader Constructor");
			_swfUrl = swfUrl;
			_type = type;
			load();
		}
		
		public function get url():String {
			return _swfUrl;
		}

		public function load() : void {
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			_loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			_loader.load(new URLRequest(_swfUrl), new LoaderContext(true, ApplicationDomain.currentDomain));
		}

		private function completeHandler(event : Event) : void {
			//trace("AssetLoader.completeHandler.swfUrl: " + _swfUrl);
			if (_loaded) return;
			onLoaded();
		}

		private function httpStatusHandler(event : HTTPStatusEvent) : void {
			//trace("AssetLoader.httpStatusHandler.swfUrl: " + _swfUrl);
			if (event.status >= 400) onFailed();
		}

		private function initHandler(event : Event) : void {
			//trace("AssetLoader.initHandler.swfUrl: " + _swfUrl);
			if (_loaded) return;
			onLoaded();
		}

		private function ioErrorHandler(event : IOErrorEvent) : void {
			trace("AssetLoader.ioErrorHandler.swfUrl: " + _swfUrl);
			dispatchEvent(new Event(FAILED));
		}

		private function openHandler(event : Event) : void {
            //trace("AssetLoader.openHandler.swfUrl: " + _swfUrl);
		}

		private function progressHandler(event : ProgressEvent) : void {
			//trace("AssetLoader.progressHandler.swfUrl: " + _swfUrl);
		}
		
		private function onLoaded():void {
			if (_type == SWF_TYPE) _swfContent = _loader.content;
			if (_type == IMG_TYPE) _imgContent = _loader.content;
			dispatchEvent(new Event(LOADED));
			_loaded = true;
		}
		
		private function onFailed():void {
			//trace("AssetLoader.onFailed.swfUrl: " + _swfUrl);
			dispatchEvent(new Event(FAILED));
		}
		
		public function get loader():Loader {
			return _loader;
		}
		
		public function get content():* {
			var value:*;
			if (_type == SWF_TYPE) value = _swfContent;
			if (_type == IMG_TYPE) value = _imgContent;
			return value;
		}
		
		public function get appDomain():ApplicationDomain {
			return _loader.contentLoaderInfo.applicationDomain;
		}
	}
}
